#include <QGraphicsView>
#include <QTimer>

#include "UIViewController.h"
#include "UIApplication.h"

class UIViewControllerPrivate {
    public:
        UIInterfaceOrientation orientation;
};

// ===========================================================================
//  PUBLIC Constructors/Destructors
// ===========================================================================
UIViewController::UIViewController (QObject *parent)
    : QObject(parent), d(new UIViewControllerPrivate)
{
    d->orientation = UIInterfaceOrientationPortrait;
    connect(UIDevice::currentDevice(), SIGNAL(orientationChanged(UIDeviceOrientation)),
            this, SLOT(orientationChanged(UIDeviceOrientation)));

    QTimer::singleShot(0, this, SLOT(initObjects()));
}

UIViewController::~UIViewController() {
    viewDidUnload();
    delete d;
}

// ===========================================================================
//  PUBLIC Properties
// ===========================================================================
UIInterfaceOrientation UIViewController::interfaceOrientation (void) const {
    return(d->orientation);
}

// ===========================================================================
//  PROTECTED Methods
// ===========================================================================
void UIViewController::viewDidLoad (void) {
}

void UIViewController::viewDidUnload (void) {
}

bool UIViewController::shouldAutorotate (UIInterfaceOrientation orientation) const {
    return(UIInterfaceOrientationIsPortrait(orientation));
}

void UIViewController::orientationChanged (void) {
}

// ===========================================================================
//  PRIVATE Slots
// ===========================================================================
void UIViewController::initObjects (void) {
    viewDidLoad();
}

#include <QDebug>
void UIViewController::orientationChanged (UIDeviceOrientation orientation) {
    if (shouldAutorotate((UIInterfaceOrientation)orientation)) {
        d->orientation = (UIInterfaceOrientation)orientation;

        // Get the ScreenView
        QGraphicsView *view = UIApplication::sharedApplication()->screenView();

        // Disable Window Repaint
        view->setUpdatesEnabled(false);

#ifndef UIKIT_SIMULATOR
        qreal y = (view->height() / 2);
        qreal x = (view->width() / 2);

        QRect screenRect;
        qreal angle = 0;
        switch (d->orientation) {
            case UIInterfaceOrientationPortrait:
                screenRect = QRect(0, 0, 480, 640);
                angle = 0;
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                screenRect = QRect(0, 0, 480, 640);
                angle = 180;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                screenRect = QRect(0, 0, 640, 480);
                angle = 90;
                break;
            case UIInterfaceOrientationLandscapeRight:
                screenRect = QRect(0, 0, 640, 480);
                angle = 270;
                break;
        }

        // Rotate the "Screen" GraphicsView        
        QTransform transformation;
        transformation.translate(x, y).rotate(angle).translate(-x, -y);
        view->setTransform(transformation);
        view->setSceneRect(screenRect);
#endif

        // Call Orientation Changed
        orientationChanged();

        // Re-Enable Window Repaint
        view->setUpdatesEnabled(true);
    }
}

